package com.yvdelx.cyclowomen.object;

import com.yvdelx.cyclowomen.utility.LogUtility;
import com.yvdelx.cyclowomen.utility.StringUtility;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.yvdelx.cyclowomen.utility.StringUtility.CST_XML_DATA_CURRENT_CYCLE;
import static com.yvdelx.cyclowomen.utility.StringUtility.CST_XML_DATA_CYCLE_ELEMENT;
import static com.yvdelx.cyclowomen.utility.StringUtility.CST_XML_DATA_DATE_DEBUT_ATTRIBUTE;
import static com.yvdelx.cyclowomen.utility.StringUtility.CST_XML_DATA_DATE_FIN_ATTRIBUTE;
import static com.yvdelx.cyclowomen.utility.StringUtility.CST_XML_DATA_DAY_ELEMENT;
import static com.yvdelx.cyclowomen.utility.StringUtility.CST_XML_DATA_ID_ATTRIBUTE;

/**
 * Object Cycle
 * Created by ydelvaux on 22/02/14.
 */
@Element(name=CST_XML_DATA_CYCLE_ELEMENT)
public class Cycle implements Comparable<Cycle> {


    @ElementList(name=CST_XML_DATA_DAY_ELEMENT,type = Day.class, inline = true)
    private List<Day> lstDays;

    @Element(name=CST_XML_DATA_ID_ATTRIBUTE, type=String.class)
    private String idCycle;

    @Element(name=CST_XML_DATA_DATE_DEBUT_ATTRIBUTE, type=Date.class)
    private Date dateDebut;

    @Element(name=CST_XML_DATA_DATE_FIN_ATTRIBUTE, type=Date.class, required = false)
    private Date dateFin;


    @Element(name=CST_XML_DATA_CURRENT_CYCLE, type = Boolean.class)
    private Boolean currentCycle;

    // These values are computed in real time and not stored by the XML framework
    private Float averageLowTemperature;
    private Float averageHighTemperature;
    private boolean isComputed = false;

    /**
     * Simple constructor for SimpleFrameworkXML
     */
    public Cycle(){
        super();
    }

    /**
     * Current constructor used in the app
     * @param idCycle String initialize the id of the cycle
     */
    public Cycle(String idCycle) {
        this.idCycle = idCycle;
        this.lstDays = new ArrayList<Day>();
    }

    public String getIdCycle() {
        return this.idCycle;
    }

    @SuppressWarnings("all")
    public void setIdCycle(String idCycle) {
        this.idCycle = idCycle;
    }

    public Boolean isCurrentCycle() {
        return this.currentCycle;
    }

    public void setCurrentCycle(Boolean currentCycle) {
        this.currentCycle = currentCycle;
    }

    public void setDateDebut(Date dateDebut) {
        this.dateDebut = dateDebut;
    }

    public void setDateFin(Date dateFin) {
        this.dateFin = dateFin;
    }

    @SuppressWarnings("all")
    public Date getDateDebut() {
        return this.dateDebut;
    }

    @SuppressWarnings("all")
    public Date getDateFin() {
        return this.dateFin;
    }

    @SuppressWarnings("all")
    public Day getDay(Integer index){
        return this.lstDays.get(index);
    }

    public Float getAverageLowTemperature() {
        return averageLowTemperature;
    }

    public Float getAverageHighTemperature() {
        if(averageHighTemperature != null)
            return averageHighTemperature;
        else
            return 0f;
    }

    public List<Day> getLstDays() {
        return lstDays;
    }

    public boolean isComputed() {
        return isComputed;
    }

    public void setLstDays(List<Day> lstDays) {
        this.lstDays = lstDays;
    }

    public void setAverageLowTemperature(Float averageLowTemperature) {
        this.averageLowTemperature = averageLowTemperature;
    }

    public void setAverageHighTemperature(Float averageHighTemperature) {
        this.averageHighTemperature = averageHighTemperature;
    }

    public void setComputed(boolean isComputed) {
        this.isComputed = isComputed;
    }

    @Override
    public boolean equals(Object o) {
        if(o!=null){
            Cycle cycleToCompare = (Cycle) o;
            return StringUtility.compareStrings(this.getIdCycle(), cycleToCompare.getIdCycle());
        } else {
            return false;
        }
    }

    @Override
    public String toString(){
        // To economize the phone battery, the app display all information only in debug mode.
        if(LogUtility.isDebug()){
            String result = "Cycle ["
                    + "ID : " + getIdCycle() + ", "
                    + "Average High Temperature : " + averageHighTemperature + ", "
                    + "Average Low Temperature : " + averageLowTemperature + ", "
                    + "Days : ";

            for(Day day : lstDays)
                result += day.toString() + " / ";

            result += "]";
            return result;
        } else {
            return this.getIdCycle();
        }
    }

    @Override @SuppressWarnings("all")
    public int compareTo(Cycle other) {
        // Cycle with the biggest ID is always after the other
        Integer id = Integer.valueOf(this.getIdCycle());
        Integer idOther = Integer.valueOf(other.getIdCycle());
        return id.compareTo(idOther);
    }
}
